package com.fira.edu.service.impl;

import java.util.List;

import com.fira.common.constant.Constants;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.StringUtils;
import com.fira.common.utils.bean.DozerUtils;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.edu.domain.EduCourseSchedule;
import com.fira.edu.domain.EduCourseScheduleTime;
import com.fira.edu.domain.EduStudentCourse;
import com.fira.edu.domain.dto.ClassRollCallDTO;
import com.fira.edu.domain.dto.StudentRollCallCountDTO;
import com.fira.edu.mapper.EduCourseScheduleMapper;
import com.fira.edu.mapper.EduCourseScheduleTimeMapper;
import com.fira.edu.service.IEduStudentCourseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fira.edu.mapper.EduClassRollCallMapper;
import com.fira.edu.domain.EduClassRollCall;
import com.fira.edu.service.IEduClassRollCallService;
import com.fira.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 上课点名记录Service业务层处理
 * 
 * @author zhushuyong
 * @date 2021-10-12
 */
@Service
public class EduClassRollCallServiceImpl implements IEduClassRollCallService 
{
    @Autowired
    private EduClassRollCallMapper eduClassRollCallMapper;
    @Autowired
    private IEduStudentCourseService eduStudentCourseService;
    @Autowired
    private EduCourseScheduleMapper eduCourseScheduleMapper;
    @Autowired
    private EduCourseScheduleTimeMapper eduCourseScheduleTimeMapper;

    /**
     * 查询上课点名记录
     * 
     * @param id 上课点名记录ID
     * @return 上课点名记录
     */
    @Override
    public EduClassRollCall selectEduClassRollCallById(String id)
    {
        return eduClassRollCallMapper.selectEduClassRollCallById(id);
    }

    /**
     * 查询上课点名记录列表
     * 
     * @param eduClassRollCall 上课点名记录
     * @return 上课点名记录
     */
    @Override
    public List<EduClassRollCall> selectEduClassRollCallList(EduClassRollCall eduClassRollCall)
    {
        return eduClassRollCallMapper.selectEduClassRollCallList(eduClassRollCall);
    }

    /**
     * 新增上课点名记录
     * 
     * @param classRollCallDTOList 上课点名记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertEduClassRollCall(List<ClassRollCallDTO> classRollCallDTOList)
    {
        //根据studentId和courseId查询studentCourseId
        classRollCallDTOList.forEach(classRollCallDTO -> {
            EduStudentCourse eduStudentCourse = new EduStudentCourse();
            eduStudentCourse.setCourseId(classRollCallDTO.getCourseId());
            eduStudentCourse.setStudentId(classRollCallDTO.getStudentId());
            classRollCallDTO.setId(IdUtils.fastSimpleUUID());
            EduClassRollCall eduClassRollCall = DozerUtils.copyProperties(classRollCallDTO,EduClassRollCall.class);
            eduClassRollCall.setCreateTime(DateUtils.getNowDate());
            List<EduStudentCourse> studentCourseList = eduStudentCourseService.selectEduStudentCourseList(eduStudentCourse);
            if(CollectionUtils.isNotEmpty(studentCourseList)){
                String studentCourseId = studentCourseList.get(0).getId();
                eduClassRollCall.setStudentCourseId(studentCourseId);
            }
            eduClassRollCallMapper.insertEduClassRollCall(eduClassRollCall);
            if(!classRollCallDTO.getToClassStatus().equals(Constants.TOCLASS_STATUS_CD) && !classRollCallDTO.getToClassStatus().equals(Constants.TOCLASS_STATUS_QX)){
                //消减课时
                EduCourseSchedule eduCourseSchedule = eduCourseScheduleMapper.selectEduCourseScheduleById(classRollCallDTOList.get(0).getCourseScheduleId());
                EduStudentCourse studentCourse = new EduStudentCourse();
                studentCourse.setStudentId(classRollCallDTO.getStudentId());
                studentCourse.setCourseId(eduCourseSchedule.getCourseId());
                studentCourse.setClassId(eduCourseSchedule.getClassId());
                studentCourse = eduStudentCourseService.selectEduStudentCourseList(studentCourse).get(0);
                studentCourse.setUnuseClassHours(studentCourse.getUnuseClassHours() - Integer.parseInt(classRollCallDTOList.get(0).getCurrentClassHours()));
                eduStudentCourseService.updateEduStudentCourse(studentCourse);
            }
        });
        //更新课次状态为已上课
        EduCourseScheduleTime courseScheduleTime = new EduCourseScheduleTime();
        courseScheduleTime.setCourseStatus(Constants.STATUS_ONE);
        courseScheduleTime.setId(classRollCallDTOList.get(0).getCourseScheduleTimeId());
        eduCourseScheduleTimeMapper.updateEduCourseScheduleTime(courseScheduleTime);

        return 1;
    }

    /**
     * 修改上课点名记录
     * 
     * @param eduClassRollCall 上课点名记录
     * @return 结果
     */
    @Override
    public int updateEduClassRollCall(EduClassRollCall eduClassRollCall)
    {
        eduClassRollCall.setUpdateTime(DateUtils.getNowDate());
        return eduClassRollCallMapper.updateEduClassRollCall(eduClassRollCall);
    }

    /**
     * 删除上课点名记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEduClassRollCallByIds(String ids)
    {
        return eduClassRollCallMapper.deleteEduClassRollCallByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除上课点名记录信息
     * 
     * @param id 上课点名记录ID
     * @return 结果
     */
    @Override
    public int deleteEduClassRollCallById(String id)
    {
        return eduClassRollCallMapper.deleteEduClassRollCallById(id);
    }

    /**
     * 查询学生到课统计数量
     *
     * @param studentRollCallCountDTO
     * @return
     */
    @Override
    public Integer selectStudentRollCallCount(StudentRollCallCountDTO studentRollCallCountDTO) {
        return eduClassRollCallMapper.selectStudentRollCallCount(studentRollCallCountDTO);
    }

    /**
     * 查询未生成通知消息的点名信息
     *
     * @return
     */
    @Override
    public List<EduClassRollCall> selectRollCallForMsg() {
        return eduClassRollCallMapper.selectRollCallForMsg();
    }
}
